<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="setting.jsp"%>
<html>
	<head>
		<title>刷单Dashboard</title>
		<style>
			table,table tr th, table tr td { border:1px solid #0094ff; }
			table { width: 200px; min-height: 25px; line-height: 25px; text-align: center; border-collapse: collapse;}
		</style>
		<link rel="stylesheet" href="https://unpkg.com/purecss@0.6.1/build/pure-min.css" integrity="sha384-CCTZv2q9I9m3UOxRLaJneXrrqKwUNOzZ6NGEUMwHtShDJ+nCoiXJCAgi05KfkLGY" crossorigin="anonymous">
		<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
		<script>
			$(function () {
				$.getJSON('${base}/getAllService',function (res) {

					var html = "";
					var rows = res.data;
					for(var i = 0;i<rows.length;i++){
					    html += '<tr>';
							var row = rows[i];
							html += '<td class="service" data-id='+row.id+'>'+row.serviceName+'</td>';
                            html += '<td><input class="schedule_type" style="width: 40px" value="'+row.scheduleType+'"/><input  class="schedule_value" value="'+row.scheduleValue+'" style="width: 40px"/>ms<button>保存</button></td>';
							html += '<td class="createTime"></td>';
                        	html += '<td class="initialContent"></td>';
							html += '<td class="progress"></td>';
							html += '<td class="stateName"></td>';
							html += '<td class="op"></td>';
                        html += '</tr>';
					}

					$('#tableContainer').html(html);

                    initService();
                });

            });
			function initService() {
				$('.service').each(function () {
				    var serviceId = $(this).data('id');

				    var $trDom = $(this).parent();

					$.getJSON('${base}/findServiceRuntime?serviceId='+serviceId,function (res) {
					    	var runtime = res.data;
					    	if(res.data.length==0){
                                $trDom.find(".op").html('<button class="pure-button pure-button-primary" onclick="start('+serviceId+')"  >开启</button>')
							}else{
                                $trDom.find(".createTime").html(runtime.createTime);
                                $trDom.find(".initialContent").html(runtime.initialContent);
                                $trDom.find(".progress").html(runtime.progress);
                                $trDom.find(".stateName").html(runtime.stateName);
                                $trDom.find(".op").html('<button class="pure-button pure-button-primary" onclick="kill('+serviceId+')"  >KILL</button><button class="pure-button pure-button-primary" onclick="restart('+serviceId+')"  >停止</button>');
							}
                    });
                });
            }
            function start(id) {
                    $.ajax({
                        type: "GET",
                        url: "${base}/submit?serviceId="+id,
						async:false,
                        dataType: "json",
                        success: function(res){
                            if(res.code==0){
                                window.location.reload();
							}else{
                                alert(res.msg);
							}
                        }
                    });
            }
            function kill(id) {
                $.ajax({
                    type: "GET",
                    url: "${base}/kill?serviceId="+id,
                    async:false,
                    dataType: "json",
                    success: function(res){
                        if(res.code==0){
                            window.location.reload();
                        }else{
                            alert(res.msg);
                        }
                    }
                });
            }
            function restart(id) {
                $.ajax({
                    type: "GET",
                    url: "${base}/restart?serviceId="+id,
                    async:false,
                    dataType: "json",
                    success: function(res){
                        if(res.code==0){
                            window.location.reload();
                        }else{
                            alert(res.msg);
                        }
                    }
                });
            }
		</script>
	</head>
	<body>
		<h1>刷单Dashboard</h1>
		<div>
			<table class="pure-table" style="width: 100%">
				<thead>
				<tr>
					<td style="width: 20%">任务</td>
					<td style="width: 10%">调度方式,延时</td>
					<td style="width: 10%">开始时间</td>
					<td style="width: 10%">此次内容</td>
					<td>进度</td>
					<td style="width: 10%">状态</td>
					<td style="width: 10%">操作</td>
				</tr>
				</thead>

				<tbody id="tableContainer">
				<!--
					<tr>
						<td>(AliExpress)物流跟踪信息</td>
						<td></td>
						<td></td>
						<td>未开始</td>
						<td>2个</td>
						<td>
							<button class="pure-button">开启</button>
							<button class="pure-button">关闭</button>
						</td>
					</tr>-->
				</tbody>
			</table>
		</div>

	</body>
</html>
